package com.xiaoge.springmybatis.springintegrationmybatis;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * TODO  spring 整合 mybatis
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
public class SpringIntegrationMybatisTwo implements FactoryBean {
	// todo FactoryBean 本身就是一个特殊bean
	private Class mapper; // 灵活设置属性

	public SpringIntegrationMybatisTwo(Class mapper) {
		this.mapper = mapper;
	}

	@Override
	public Object getObject() throws Exception {
		// mybatis 代理对象
		Object o = Proxy.newProxyInstance(SpringIntegrationMybatisTwo.class.getClassLoader(), new Class[]{mapper}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				if (Object.class.equals(method.getDeclaringClass())) {
					System.out.println(method.getName());
					return method.invoke(this, args);
				}
				System.out.println(method.getName());
				return null;
			}
		});
		return o; // bean
	}

	@Override
	public Class<?> getObjectType() {
		return mapper;
	}
}
